package ext.tianma.bom.service.imp;



import javax.jws.WebService;

import org.apache.log4j.Logger;

import wt.change2.WTChangeOrder2;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.log4j.LogR;
import wt.pds.StatementSpec;
import wt.query.ClassAttribute;
import wt.query.OrderBy;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.workflow.engine.ProcessData;
import wt.workflow.engine.WfProcess;
import wt.workflow.engine.WfState;

@WebService(endpointInterface = "ext.cust.integration.ECNService",
targetNamespace = "http://seed.plm.com/ECNService",
serviceName = "ECNService", portName = "ECNSOAPPort")
public class ECNImpl implements ECNService {
    private final static Logger LOGGER = LogR.getLogger(ECNImpl.class.getName());

    @Override
    public String erpFeedback(String ecnNumber, boolean status, String message) {
    	System.out.println("==========erpFeedback 2======================="+ecnNumber+"==status:"+"===message:"+message);
        String result = "";
        WTChangeOrder2 order = null;
        try {
            order = getECNByNumber(ecnNumber);
        } catch (WTException e) {
            e.printStackTrace();
        }
        
        if(order == null){
            result = "can't find ECN : " + ecnNumber;
        }
        
        if(order != null){
            WfProcess process = null;
            try {
                process = getLatestProcess(order);
            } catch (WTException e1) {
                e1.printStackTrace();
            }
            if(process == null){
                result = "can find running process for ECN : " + ecnNumber;
            }else{
                System.out.println("********ECN process : " + process.toString());
                
                ProcessData processData = process.getContext();
                if(processData == null){
                    return "get process context failed";
                }
                
                String feedbackStatus = "";
                String feedbackMessage = "";
                if(status){
                    feedbackStatus = "success";
                }else{
                    feedbackStatus = "failed";
                    feedbackMessage = message;
                }
                
                try {
                    System.out.println("******begin set variable in ECNImpl");
                    processData.setValue("invokeFlag", true);
                    processData.setValue("feedbackStatus", feedbackStatus);
                    processData.setValue("feedbackMessage", feedbackMessage);
                    process.setContext(processData);
                    
                    PersistenceHelper.manager.save(process);
                    
                    result = "success";
                } catch (WTException e) {
                    e.printStackTrace();
                    result = "set process variable failed";
                }       
                
                processData = process.getContext();
                
                System.out.println("********invokeFlag="+
                        processData.getValue("invokeFlag") + " status="+processData.getValue("feedbackStatus"));
            }
        }
        
        return result;
    }
    
    private WTChangeOrder2 getECNByNumber(String number) throws WTException{
        QuerySpec query = new QuerySpec(WTChangeOrder2.class);
        query.appendWhere(
                new SearchCondition(WTChangeOrder2.class,"master>number", SearchCondition.EQUAL, number),
                new int[]{0});
        QueryResult result = PersistenceHelper.manager.find((StatementSpec)query);
        if(result.hasMoreElements()){
            WTChangeOrder2 order2 = (WTChangeOrder2)result.nextElement();
            return order2;
        }
        return null;
    }
    
    private WfProcess getLatestProcess(WTChangeOrder2 order) throws WTException{
        QuerySpec query = new QuerySpec(WfProcess.class);
        String pbo = "VR:wt.change2.WTChangeOrder2:" + order.getBranchIdentifier();
        query.appendWhere(
                new SearchCondition(WfProcess.class,"businessObjReference", SearchCondition.EQUAL, pbo),
                new int[]{0});
        query.appendAnd();
        query.appendWhere(
                new SearchCondition(WfProcess.class,"state", SearchCondition.EQUAL, WfState.OPEN_RUNNING),
                new int[]{0});
        
        OrderBy orderby = new OrderBy(new ClassAttribute(WfProcess.class, "thePersistInfo.createStamp"), true);
        query.appendOrderBy(orderby, new int[] { 0 });
        QueryResult result = PersistenceHelper.manager.find((StatementSpec)query);
        if(result.hasMoreElements()){
            WfProcess process = (WfProcess)result.nextElement();
            return process;
        }
        return null;
    }
    
   
}
